home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 2004 #11
/
Amiga Plus CD - 2004 - No. 11.iso
/
AmiSoft
/
Util
/
crypt
/
sign7800.lha
/
sign7800.txt
< prev
Wrap
Text File
|
2004-06-20
|
5KB
|
99 lines
This program will generate a valid digital signature for an Atari 7800
cartridge ROM image.
The code was created by hand-decompiling the original 7800 encryption
program for the Atari ST into plain C code, so it does not rely on any
external libraries and should compile for almost any operating system
with no muss and no fuss.
To build it:
============
gcc sign7800.c -o sign7800
To run it:
==========
sign7800 -t filename to test for a valid existing signature only
sign7800 filename to generate a signature printed to stdout
sign7800 -w filename to generate a signature and update the image
'Filename' is the name of the file with the cartridge data in it. It
must be a binary file, with a size that is a multiple of 4K bytes. An
extra 128 bytes is allowed, so that you can use it with a .A78 file.
The '-t' option only tests to see if an existing signature is valid.
The '-w' option causes the file to be updated with a new signature if
it was not aready valid. A write will not happen if there is a problem
with $FFF8, $FFF9, or the reset vector, or if there is already a valid
signature in the file. Even with an already valid signature, a new
signature is still computed and printed to stdout.
WARNING: using the '-w' option should be safe if your file is really a 7800
cartridge image, but if it isn't, there is a possibility that data loss
to the last 128 bytes of the file could occur! Be sure that you have a
backup copy of or can re-create whatever file you use with the '-w' option!
The rules for making a cartridge start up in Atari 7800 mode:
=============================================================
The encrypted cartridge hash at $FF80-$FFF7 must be valid. (This will not
be necessary with a European 7800 console, as they do not have the crypto
check.)
The high nibble of $FFF8 must be $F.
The low bit of $FFF8 must be set. This was at some time intended for a
region setting, with the USA apparently being the low bit. So just set
$FFF8 to $FF (which should mean "all regions"), and forget about it.
The high nibble of $FFF9 must be $4-$F, and should not include the bank switch
memory range of a bank-switched cartridge. This should be set to $F for
fastest startup whenever possible.
The low nibble of $FFF9 must be 3 or 7. 3 will disable the k3wl Atari
rainbow startup graphics, but won't make the game start any faster. So use 7.
The reset vector at $FFFC-$FFFD must point into the hashed range.
Technical mumbo-jumbo:
======================
The Atari 7800 encryption is based upon a hash generated from the cartridge
data. This hash is then encrypted (using the private key of P and Q which
were contained in the Atari ST program) and stored in the cartridge at
addresses $FF80-$FFF8. At startup, the Atari 7800 runs its own hash of the
cartridge data, then it decrypts the signature using the public key, N. If
the hash does not match the decrypted signature, the 7800 will start up the
cartridge in Atari 2600 mode.
Because the signature algorithm doesn't always have a valid solution, the
fourth byte in the hash is not checked for a match. Instead, it is changed
until a valid solution is found. This usually takes four or less attempts
at encryption, though I have seen it take as many as twelve.
The page range to be hashed will be a multiple of 4K, up to 48K. This
is specified in the cartridge at address $FFF9. The high nibble of this
byte refers to which page to start the hash. The $FF00 page is handled
specially, by zeroing out the bytes which will receive the signature.
One important thing to note is that the 7800 validates that the reset vector
points within the hashed page range. This was to prevent the making (either
accidentally or intentionally) of a "magic key" game in which the encrypted
range can be copied verbatim and otherwise ignored. In fact, Harry Dodgson's
monitor cartridge can be used this way, as only 4K is hashed, and the reset
vector points to a jump to outside of the hashed range.
Before generating the signature for a cartridge, the high nibble of $FFF9
must be set. Since the hash is slow enough on a 7800 that you can notice
the difference in startup time between a 4K hash and a 48K hash, it is
recommended for newly written games to always set this byte to $F0 and simply
make sure that the reset vector is in the $Fxxx range.
Also, when signing bank-switched games, $FFF9 should never be less than $C0
to ensure that the hash will not be dependent upon the bank switch area.